//
//  WodejinengViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/14.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "WodejinengViewController.h"
#import "JinengSelectViewController.h"
#import "WodejinengTableViewCell.h"

@interface WodejinengViewController ()<UITableViewDataSource,UITableViewDelegate,jinenngSelectDelegate,wojinenngDCellDelegate>
{
    UITableView * contentTableView;
    UIButton * noJineng;
    NSArray * jinengData;
    NSArray * unsoldData;
}
@end

@implementation WodejinengViewController


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.title = @"我的技能";
    
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    noJineng = [[UIButton alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, 30)];
    [noJineng setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    noJineng.titleLabel.font = [UIFont systemFontOfSize:12];
    [noJineng setTitle:@"技能不存在？去反馈" forState:UIControlStateNormal];
    [noJineng addTarget:self action:@selector(nojinengAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:noJineng];
    
    UIBarButtonItem * addButton = [[UIBarButtonItem alloc]initWithTitle:@"添加技能" style:UIBarButtonItemStylePlain target:self action:@selector(addJineng)];
    self.navigationItem.rightBarButtonItem = addButton;

    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectNav.size.height + rectStatus.size.height + 30, ScreenWidth, ScreenHeight - rectNav.size.height - rectStatus.size.height - 30) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self request];
}

-(void)request
{
    [NetRequestCenter soldabilityResult:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            jinengData = [[resultDic objectForKey:@"data"]objectForKey:@"CouldSold"];
            unsoldData = [[resultDic objectForKey:@"data"]objectForKey:@"UnSold"];
            [contentTableView reloadData];
        }
    }];
}

-(void)addJineng
{
    JinengSelectViewController * jineng = [[JinengSelectViewController alloc]init];
    [self presentViewController:[[MMNavController alloc]initWithRootViewController:jineng] animated:YES completion:nil];
}

-(void)nojinengAction
{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"你需要的能力"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         //响应事件
                                                         //得到文本信息
                                                         for(UITextField *text in alert.textFields){
                                                             NSLog(@"text = %@", text.text);
                                                             [SVProgressHUD show];
                                                             [NetRequestCenter delprivatewithtitle:@"" content:text.text result:^(BOOL success, NSDictionary *resultDic) {
                                                                 if (success) {
                                                                     [SVProgressHUD showSuccessWithStatus:@"成功"];
                                                                 }
                                                                 else
                                                                 {
                                                                     [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"messgae"]];
                                                                 }
                                                             }];
                                                         }
                                                     }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", alert.textFields);
                                                         }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"请输入内容";
    }];

    
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];

}

-(void)selectedJineng:(NSDictionary *)jineng
{
    
}

#pragma mark - tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return [jinengData count];
    }
    else
    {
        return [unsoldData count];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [WodejinengTableViewCell height];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifer = @"identifer";
    WodejinengTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[WodejinengTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.delegate = self;
    if (indexPath.section == 0)
    {
        [cell loadData:[jinengData objectAtIndex:indexPath.row] sold:YES];
    }
    else
    {
        [cell loadData:[unsoldData objectAtIndex:indexPath.row] sold:NO];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)addAbility:(NSDictionary *)data
{
    [SVProgressHUD show];
    [NetRequestCenter addsoldabilityWithClassid:[data objectForKey:@"LevelId"] result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [self request];
            [SVProgressHUD showSuccessWithStatus:@"成功"];
        }
    }];
}

-(void)removeAbility:(NSDictionary *)data
{
    [SVProgressHUD show];
    [NetRequestCenter removesoldabilityWithClassid:[data objectForKey:@"LevelId"] result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [self request];
            [SVProgressHUD showSuccessWithStatus:@"成功"];
        }
    }];
}

-(void)deleteCurrent:(NSDictionary *)data
{
    [SVProgressHUD show];
    [NetRequestCenter removeabilityWithClassid:[data objectForKey:@"LevelId"] result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [self request];
            [SVProgressHUD showSuccessWithStatus:@"成功"];
        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
